﻿using LinqForEEPlus;
using MatlabWork.Dal;
using MatlabWork.Entity;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using TestingPlatForm.MissionContrl.Entity;

namespace MatlabWork.Model
{
    public class ControlExcel
    {
        public static string currentDirectory = Directory.GetCurrentDirectory();
        public static string directiory = currentDirectory + "\\outPut";

        public bool CopyExcel(string extension)
        {
            bool result = false;
            string sourceFilePath = directiory + "\\gm.xlsx";
            string destinationFolderPath = directiory;
            string newFileName = extension;

            // 复制文件
            string destinationFilePath = Path.Combine(destinationFolderPath, newFileName);
            File.Copy(sourceFilePath, destinationFilePath);

            // 重命名文件
            File.Move(destinationFilePath, Path.Combine(destinationFolderPath, extension + ".xlsx"));

            result = true;
            return result;

        }

        public Dictionary<string, Titude> readExcel(string filename)
        {
            Calculate calculate = new Calculate();
            Dictionary<string, Titude> result = new Dictionary<string, Titude>();
            using (ExcelPackage package = new ExcelPackage(new FileInfo(filename)))
            {
                ExcelWorkbook workbook = package.Workbook;
                if (workbook != null)
                {
                    ExcelWorksheet worksheet = workbook.Worksheets[0];
                    int rowCount = worksheet.Dimension.Rows;                    
                    for (int i = 1; i <= rowCount; i++)
                    {
                        Titude titude = new Titude();
                        titude.latitude = double.Parse(worksheet.Cells[i, 2].Value.ToString());
                        titude.longitude = double.Parse(worksheet.Cells[i, 3].Value.ToString());
                        titude.altitude = double.Parse(worksheet.Cells[i, 4].Value.ToString());
                        titude = calculate.ConvertGeodeticToECEF(titude);
                        result[worksheet.Cells[i, 1].Value.ToString()] = titude;
                    }
                }
            }
            return  result;
        }

        public string CreateFold(string filename)
        {
            //string filename = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
            string folderPath = @directiory + "\\" + filename; // 指定要创建的文件夹路径

            try
            {
                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath); // 调用 CreateDirectory 方法创建文件夹
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生错误：{ex.Message}");
            }

           return folderPath;
        }
        public bool insertExcel(string folderPath, string FILENAME)
        {
            bool result = false;
            string directiory = folderPath;

            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            // 创建或打开Excel文件
            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                // 如果文件不存在，则创建新的工作簿和工作表
                if (!fileExists)
                {
                    ExcelWorkbook workbook = package.Workbook;
                    ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");

                    // 写入标题行
                    worksheet.Cells["A1"].Value = "; Propsim Aerospace Model file ";
                    worksheet.Cells["B1"].Value = " version 1.0 ";
                    worksheet.Cells["A3"].Value = "[Model] ";
                    worksheet.Cells["A4"].Value = "SimulationCenterFrequency = 18000000000 Hz ";
                    worksheet.Cells["A5"].Value = "RFCenterFrequency = 1800000000 Hz ";
                    worksheet.Cells["A6"].Value = "[LOS] ";

                }

                // 保存Excel文件
                package.Save();
                result = true;
            }
            return result;
        }

        public bool insertExcel127(string folderPath, string FILENAME)
        {
            bool result = false;
            string directiory = folderPath;

            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            // 创建或打开Excel文件
            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                // 如果文件不存在，则创建新的工作簿和工作表
                if (!fileExists)
                {
                    ExcelWorkbook workbook = package.Workbook;
                    ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");

                    // 写入标题行
                    worksheet.Cells["A1"].Value = "; Propsim Aerospace Model file ";
                    worksheet.Cells["B1"].Value = " version 1.0 ";
                    worksheet.Cells["A3"].Value = "[Model] ";
                    worksheet.Cells["A4"].Value = "SimulationCenterFrequency = 12700000000 Hz ";
                    worksheet.Cells["A5"].Value = "RFCenterFrequency = 1800000000 Hz ";
                    worksheet.Cells["A6"].Value = "[LOS] ";

                }
                
                // 保存Excel文件
                package.Save();
                result = true;
            }
            return result;
        }

        public bool insertExcel472(string folderPath, string FILENAME)
        {
            bool result = false;
            string directiory = folderPath;

            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            // 创建或打开Excel文件
            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                // 如果文件不存在，则创建新的工作簿和工作表
                if (!fileExists)
                {
                    ExcelWorkbook workbook = package.Workbook;
                    ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");

                    // 写入标题行
                    worksheet.Cells["A1"].Value = "; Propsim Aerospace Model file ";
                    worksheet.Cells["B1"].Value = " version 1.0 ";
                    worksheet.Cells["A3"].Value = "[Model] ";
                    worksheet.Cells["A4"].Value = "SimulationCenterFrequency = 47200000000 Hz ";
                    worksheet.Cells["A5"].Value = "RFCenterFrequency = 1800000000 Hz ";
                    worksheet.Cells["A6"].Value = "[LOS] ";

                }

                // 保存Excel文件
                package.Save();
                result = true;
            }
            return result;
        }

        public void insertExcel(string folderPath, Dictionary<string, Titude> valuePairs)
        {
           
            // 创建或打开Excel文件
            using (ExcelPackage package = new ExcelPackage(new FileInfo(folderPath)))
            {
                ExcelWorkbook workbook = package.Workbook;
                if (workbook != null)
                {
                    ExcelWorksheet worksheet = workbook.Worksheets[0];
                    int rowCount = worksheet.Dimension.Rows;


                    for (int i = 1; i <= rowCount; i++)
                    {
                        worksheet.Cells[i, 5].Value = valuePairs[worksheet.Cells[i, 1].Value.ToString()].X;
                        worksheet.Cells[i, 6].Value = valuePairs[worksheet.Cells[i, 1].Value.ToString()].Y;
                        worksheet.Cells[i, 7].Value = valuePairs[worksheet.Cells[i, 1].Value.ToString()].Z;
                    }
                }
                // 保存Excel文件
                package.Save();
            }
        }

        public void insertExcel(string folderPath,string FILENAME, List<numTest> numlist)
        {
            string directiory = folderPath;

            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            // 创建或打开Excel文件
            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                // 如果文件不存在，则创建新的工作簿和工作表
                if (!fileExists)
                {
                    ExcelWorkbook workbook = package.Workbook;
                    ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");

                    // 写入标题行
                    worksheet.Cells["A1"].Value = "EK";
                    worksheet.Cells["B1"].Value = "X";
                    worksheet.Cells["C1"].Value = "Y";
                    worksheet.Cells["D1"].Value = "Z";
                    worksheet.Cells["E1"].Value = "VX";
                    worksheet.Cells["F1"].Value = "VY";
                    worksheet.Cells["G1"].Value = "VZ";
                    worksheet.Cells["H1"].Value = "NUM";
                }

                // 获取工作表
                ExcelWorksheet existingWorksheet = package.Workbook.Worksheets["Sheet1"];

                // 获取下一个空行的行号
                int nextRow = existingWorksheet.Dimension.Rows + 1;

                foreach (numTest value in numlist)
                {
                    existingWorksheet.Cells[nextRow, 1].Value = value.EK;
                    existingWorksheet.Cells[nextRow, 2].Value = value.x;
                    existingWorksheet.Cells[nextRow, 3].Value = value.y;
                    existingWorksheet.Cells[nextRow, 4].Value = value.z;
                    existingWorksheet.Cells[nextRow, 5].Value = value.vx;
                    existingWorksheet.Cells[nextRow, 6].Value = value.vy;
                    existingWorksheet.Cells[nextRow, 7].Value = value.vz;
                    existingWorksheet.Cells[nextRow, 8].Value = value.can_sec;

                    nextRow++;
                }

                // 保存Excel文件
                package.Save();
            }
        }
        public void insertRAHIntoExcel(string folderPath,string FILENAME, List<numTest> numlist , List<downLinkData> downLinkDatas, List<UpLinkData> upLinkDatas)
        {
            string directiory = folderPath;

            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            // 创建或打开Excel文件
            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                // 如果文件不存在，则创建新的工作簿和工作表
                if (!fileExists)
                {
                    ExcelWorkbook workbook = package.Workbook;
                    ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");
                    // 写入标题行
                    worksheet.Cells["A1"].Value = "X";
                    worksheet.Cells["B1"].Value = "Y";
                    worksheet.Cells["C1"].Value = "Z";
                    worksheet.Cells["D1"].Value = "VX";
                    worksheet.Cells["E1"].Value = "VY";
                    worksheet.Cells["F1"].Value = "VZ";
                    worksheet.Cells["G1"].Value = "ElevationDown";
                    worksheet.Cells["H1"].Value = "ElevationUp";
                    worksheet.Cells["I1"].Value = "方位角";
                    worksheet.Cells["J1"].Value = "卫星-终端距离";
                    worksheet.Cells["K1"].Value = "Ku上行";
                    worksheet.Cells["L1"].Value = "Ku下行";
                    worksheet.Cells["M1"].Value = "卫星-基站距离";
                    worksheet.Cells["N1"].Value = "Qv上行";
                    worksheet.Cells["O1"].Value = "Qv下行";
                    worksheet.Cells["P1"].Value = "Qv随路";
                    worksheet.Cells["Q1"].Value = "信关站上行输出EIRP(dBW)";
                    worksheet.Cells["R1"].Value = "载荷馈电接收端信噪比值(dB)";
                    worksheet.Cells["S1"].Value = "终端接收中频功率(dBm)";
                    worksheet.Cells["T1"].Value = "终端下行接收信噪比值(dB)";
                    worksheet.Cells["U1"].Value = "前向链路C/(N+I)信干噪比值（dB）";
                    worksheet.Cells["V1"].Value = "终端输出EIRP(dBw)";
                    worksheet.Cells["W1"].Value = "载荷相控阵接收端单波束PFD(dBw/m2)";
                    worksheet.Cells["X1"].Value = "载荷QV天线发射EIRP值(dBw)";
                    worksheet.Cells["Y1"].Value = "载荷相控阵接收端信噪比（dB）";
                    worksheet.Cells["Z1"].Value = "信关站接收中频功率（dBm)";
                    worksheet.Cells["AA1"].Value = "信关站下行接收信噪比(dB)";
                    worksheet.Cells["AB1"].Value = "反向链路C/(N+I)信干噪比值（dB)";
                    worksheet.Cells["AC1"].Value = "Time";

                }

                // 获取工作表
                ExcelWorksheet existingWorksheet = package.Workbook.Worksheets["Sheet1"];

                // 获取下一个空行的行号
                int nextRow = existingWorksheet.Dimension.Rows + 1;

                for (int i = 0; i < numlist.Count(); i++)
                {
                    existingWorksheet.Cells[nextRow, 1].Value = numlist[i].x;
                    existingWorksheet.Cells[nextRow, 2].Value = numlist[i].y;
                    existingWorksheet.Cells[nextRow, 3].Value = numlist[i].z;
                    existingWorksheet.Cells[nextRow, 4].Value = numlist[i].vx;
                    existingWorksheet.Cells[nextRow, 5].Value = numlist[i].vy;
                    existingWorksheet.Cells[nextRow, 6].Value = numlist[i].vz;
                    existingWorksheet.Cells[nextRow, 7].Value = numlist[i].ElevationDown;
                    existingWorksheet.Cells[nextRow, 8].Value = numlist[i].ElevationUp;
                    existingWorksheet.Cells[nextRow, 9].Value = numlist[i].Azimuth;
                    existingWorksheet.Cells[nextRow, 10].Value = numlist[i].lu_distance;
                    existingWorksheet.Cells[nextRow, 11].Value = numlist[i].ku_up;
                    existingWorksheet.Cells[nextRow, 12].Value = numlist[i].ku_down;
                    existingWorksheet.Cells[nextRow, 13].Value = numlist[i].lf_distance;
                    existingWorksheet.Cells[nextRow, 14].Value = numlist[i].qv_up;
                    existingWorksheet.Cells[nextRow, 15].Value = numlist[i].qv_down;
                    existingWorksheet.Cells[nextRow, 16].Value = numlist[i].qv_sway;
                    existingWorksheet.Cells[nextRow, 17].Value = downLinkDatas[i].EIRP;
                    existingWorksheet.Cells[nextRow, 18].Value = downLinkDatas[i].SNR;
                    existingWorksheet.Cells[nextRow, 19].Value = downLinkDatas[i].IFpower;
                    existingWorksheet.Cells[nextRow, 20].Value = downLinkDatas[i].downSNR;
                    existingWorksheet.Cells[nextRow, 21].Value = downLinkDatas[i].dryingRatio;
                    existingWorksheet.Cells[nextRow, 22].Value = upLinkDatas[i].EIRP;
                    existingWorksheet.Cells[nextRow, 23].Value = upLinkDatas[i].FPD;
                    existingWorksheet.Cells[nextRow, 24].Value = upLinkDatas[i].QVEIRP;
                    existingWorksheet.Cells[nextRow, 25].Value = upLinkDatas[i].receiveSNR;
                    existingWorksheet.Cells[nextRow, 26].Value = upLinkDatas[i].IFpower;
                    existingWorksheet.Cells[nextRow, 27].Value = upLinkDatas[i].downSNR;
                    existingWorksheet.Cells[nextRow, 28].Value = upLinkDatas[i].dryRatio;
                    existingWorksheet.Cells[nextRow, 29].Value = numlist[i].Time.ToString();
                    nextRow++;
                }

                // 保存Excel文件
                package.Save();
            }
        }

        public void insertIntoExcel2Sheet(string folderPath, string FILENAME, List<numTest> numlist, List<downLinkData> downLinkDatas, List<UpLinkData> upLinkDatas, List<TD> tDs)
        {
            string directiory = folderPath;

            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            // 创建或打开Excel文件
            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                // 如果文件不存在，则创建新的工作簿和工作表
                if (!fileExists)
                {
                    ExcelWorkbook workbook = package.Workbook;
                    ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");
                    // 写入标题行
                    worksheet.Cells["A1"].Value = "X";
                    worksheet.Cells["B1"].Value = "Y";
                    worksheet.Cells["C1"].Value = "Z";
                    worksheet.Cells["D1"].Value = "VX";
                    worksheet.Cells["E1"].Value = "VY";
                    worksheet.Cells["F1"].Value = "VZ";
                    worksheet.Cells["G1"].Value = "ElevationDown";
                    worksheet.Cells["H1"].Value = "ElevationUp";
                    worksheet.Cells["I1"].Value = "方位角";
                    worksheet.Cells["J1"].Value = "卫星-终端距离";
                    worksheet.Cells["K1"].Value = "Ku上行";
                    worksheet.Cells["L1"].Value = "Ku下行";
                    worksheet.Cells["M1"].Value = "卫星-基站距离";
                    worksheet.Cells["N1"].Value = "Qv上行";
                    worksheet.Cells["O1"].Value = "Qv下行";
                    worksheet.Cells["P1"].Value = "Qv随路";
                    worksheet.Cells["Q1"].Value = "多普勒频偏";
                    worksheet.Cells["R1"].Value = "Time";

                    worksheet = workbook.Worksheets.Add("前向");
                    worksheet.Cells["A1"].Value = "信关站工作仰角（°）";
                    worksheet.Cells["B1"].Value = "终端工作仰角（°）";
                    worksheet.Cells["C1"].Value = "馈电上行链路空间衰减（dB）";
                    worksheet.Cells["D1"].Value = "用户下行链路空间衰减(dB)";
                    worksheet.Cells["E1"].Value = "信关站上行输出EIRP(dBW)";
                    worksheet.Cells["F1"].Value = "载荷馈电接收端信噪比值(dB)";
                    worksheet.Cells["G1"].Value = "终端接收中频功率(dBm)";
                    worksheet.Cells["H1"].Value = "终端下行接收信噪比值(dB)";
                    worksheet.Cells["I1"].Value = "前向链路C/(N+I)信干噪比值（dB）";
                    worksheet.Cells["J1"].Value = "Time";
                    worksheet = workbook.Worksheets.Add("反向");
                    worksheet.Cells["A1"].Value = "信关站工作仰角（°）";
                    worksheet.Cells["B1"].Value = "终端工作仰角（°）";
                    worksheet.Cells["C1"].Value = "馈电下行链路空间衰减(dB)";
                    worksheet.Cells["D1"].Value = "用户上行链路空间衰减(dB)";
                    worksheet.Cells["E1"].Value = "终端输出EIRP(dBw)";
                    worksheet.Cells["F1"].Value = "载荷相控阵接收端单波束PFD(dBw/m2)";
                    worksheet.Cells["G1"].Value = "载荷QV天线发射EIRP值(dBw)";
                    worksheet.Cells["H1"].Value = "载荷相控阵接收端信噪比（dB）";
                    worksheet.Cells["I1"].Value = "信关站接收中频功率（dBm)";
                    worksheet.Cells["J1"].Value = "信关站下行接收信噪比(dB)";
                    worksheet.Cells["K1"].Value = "反向链路C/(N+I)信干噪比值（dB)";
                    worksheet.Cells["L1"].Value = "Time";

                }

                // 获取工作表
                ExcelWorksheet existingWorksheet = package.Workbook.Worksheets["Sheet1"];

                // 获取下一个空行的行号
                int nextRow = existingWorksheet.Dimension.Rows + 1;

                for (int i = 0; i < numlist.Count(); i++)
                {
                    existingWorksheet.Cells[nextRow, 1].Value = numlist[i].x;
                    existingWorksheet.Cells[nextRow, 2].Value = numlist[i].y;
                    existingWorksheet.Cells[nextRow, 3].Value = numlist[i].z;
                    existingWorksheet.Cells[nextRow, 4].Value = numlist[i].vx;
                    existingWorksheet.Cells[nextRow, 5].Value = numlist[i].vy;
                    existingWorksheet.Cells[nextRow, 6].Value = numlist[i].vz;
                    existingWorksheet.Cells[nextRow, 7].Value = numlist[i].ElevationDown;
                    existingWorksheet.Cells[nextRow, 8].Value = numlist[i].ElevationUp;
                    existingWorksheet.Cells[nextRow, 9].Value = numlist[i].Azimuth;
                    existingWorksheet.Cells[nextRow, 10].Value = numlist[i].lu_distance;
                    existingWorksheet.Cells[nextRow, 11].Value = numlist[i].ku_up;
                    existingWorksheet.Cells[nextRow, 12].Value = numlist[i].ku_down;
                    existingWorksheet.Cells[nextRow, 13].Value = numlist[i].lf_distance;
                    existingWorksheet.Cells[nextRow, 14].Value = numlist[i].qv_up;
                    existingWorksheet.Cells[nextRow, 15].Value = numlist[i].qv_down;
                    existingWorksheet.Cells[nextRow, 16].Value = numlist[i].qv_sway;
                    existingWorksheet.Cells[nextRow, 17].Value = tDs[i].number;
                    existingWorksheet.Cells[nextRow, 18].Value = numlist[i].Time.ToString();
                    nextRow++;
                }

                existingWorksheet = package.Workbook.Worksheets["前向"];

                // 获取下一个空行的行号
                nextRow = existingWorksheet.Dimension.Rows + 1;

                for (int i = 0; i < downLinkDatas.Count(); i++)
                {
                    existingWorksheet.Cells[nextRow, 1].Value = numlist[i].ElevationDown;
                    existingWorksheet.Cells[nextRow, 2].Value = numlist[i].ElevationDown;
                    existingWorksheet.Cells[nextRow, 3].Value = numlist[i].qv_up;
                    existingWorksheet.Cells[nextRow, 4].Value = numlist[i].ku_down;
                    existingWorksheet.Cells[nextRow, 5].Value = downLinkDatas[i].EIRP;
                    existingWorksheet.Cells[nextRow, 6].Value = downLinkDatas[i].SNR;
                    existingWorksheet.Cells[nextRow, 7].Value = downLinkDatas[i].IFpower;
                    existingWorksheet.Cells[nextRow, 8].Value = downLinkDatas[i].downSNR;
                    existingWorksheet.Cells[nextRow, 9].Value = downLinkDatas[i].dryingRatio;
                    existingWorksheet.Cells[nextRow, 10].Value = numlist[i].Time.ToString();                   
                    nextRow++;
                }

                existingWorksheet = package.Workbook.Worksheets["反向"];

                // 获取下一个空行的行号
                nextRow = existingWorksheet.Dimension.Rows + 1;

                for (int i = 0; i < upLinkDatas.Count(); i++)
                {
                    existingWorksheet.Cells[nextRow, 1].Value = numlist[i].ElevationDown;
                    existingWorksheet.Cells[nextRow, 2].Value = numlist[i].ElevationDown;
                    existingWorksheet.Cells[nextRow, 3].Value = numlist[i].qv_down;
                    existingWorksheet.Cells[nextRow, 4].Value = numlist[i].ku_up;
                    existingWorksheet.Cells[nextRow, 5].Value = upLinkDatas[i].EIRP;
                    existingWorksheet.Cells[nextRow, 6].Value = upLinkDatas[i].FPD;
                    existingWorksheet.Cells[nextRow, 7].Value = upLinkDatas[i].QVEIRP;
                    existingWorksheet.Cells[nextRow, 8].Value = upLinkDatas[i].receiveSNR;
                    existingWorksheet.Cells[nextRow, 9].Value = upLinkDatas[i].IFpower;
                    existingWorksheet.Cells[nextRow, 10].Value = upLinkDatas[i].downSNR;
                    existingWorksheet.Cells[nextRow, 11].Value = upLinkDatas[i].dryRatio;
                    existingWorksheet.Cells[nextRow, 12].Value = numlist[i].Time.ToString();                  
                    nextRow++;
                }

                // 保存Excel文件
                package.Save();
            }
        }

        public void insertIntoExcel2Sheet(string folderPath, string FILENAME, List<numTest> numlist, List<downLinkData> downLinkDatas, List<UpLinkData> upLinkDatas, List<SwayData> swayDatas, List<TD> uptDs, List<TD> downtDs, int start, int end)
        {
            string directiory = folderPath;

            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            // 创建或打开Excel文件
            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                // 如果文件不存在，则创建新的工作簿和工作表
                if (!fileExists)
                {
                    ExcelWorkbook workbook = package.Workbook;
                    ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");
                    // 写入标题行
                    worksheet.Cells["A1"].Value = "X";
                    worksheet.Cells["B1"].Value = "Y";
                    worksheet.Cells["C1"].Value = "Z";
                    worksheet.Cells["D1"].Value = "VX";
                    worksheet.Cells["E1"].Value = "VY";
                    worksheet.Cells["F1"].Value = "VZ";
                    worksheet.Cells["G1"].Value = "ElevationDown";
                    worksheet.Cells["H1"].Value = "ElevationUp";
                    worksheet.Cells["I1"].Value = "方位角";
                    worksheet.Cells["J1"].Value = "卫星-终端距离";
                    worksheet.Cells["K1"].Value = "Ku上行";
                    worksheet.Cells["L1"].Value = "Ku下行";
                    worksheet.Cells["M1"].Value = "卫星-基站距离";
                    worksheet.Cells["N1"].Value = "Qv上行";
                    worksheet.Cells["O1"].Value = "Qv下行";
                    worksheet.Cells["P1"].Value = "Qv随路";
                    worksheet.Cells["Q1"].Value = "Time";

                    worksheet = workbook.Worksheets.Add("前向");
                    worksheet.Cells["A1"].Value = "信关站工作仰角（°）";
                    worksheet.Cells["B1"].Value = "地面站-卫星距离(km)";
                    worksheet.Cells["C1"].Value = "终端工作仰角（°）";
                    worksheet.Cells["D1"].Value = "终端-卫星距离(km)";
                    worksheet.Cells["E1"].Value = "馈电上行链路空间衰减（dB）";
                    worksheet.Cells["F1"].Value = "用户下行链路空间衰减(dB)";
                    worksheet.Cells["G1"].Value = "信关站上行功放输出功率(dBm)";
                    worksheet.Cells["H1"].Value = "信关站上行输出EIRP(dBW)";
                    worksheet.Cells["I1"].Value = "载荷馈电接收端信噪比值(dB)";
                    worksheet.Cells["J1"].Value = "载荷相控阵发射EIRP(dBw）";
                    worksheet.Cells["K1"].Value = "相控阵天线接收增益（dB）";
                    worksheet.Cells["L1"].Value = "终端接收中频功率(dBm)";
                    worksheet.Cells["M1"].Value = "终端下行接收信噪比值(dB)";
                    worksheet.Cells["N1"].Value = "前向链路C/(N+I)信干噪比值（dB）";
                    worksheet.Cells["O1"].Value = "前向多普勒频偏";
                    worksheet.Cells["P1"].Value = "时间";
                    worksheet = workbook.Worksheets.Add("反向");
                    worksheet.Cells["A1"].Value = "信关站工作仰角（°）";
                    worksheet.Cells["B1"].Value = "地面站-卫星距离(km)";
                    worksheet.Cells["C1"].Value = "终端工作仰角（°）";
                    worksheet.Cells["D1"].Value = "终端-卫星距离(km)";
                    worksheet.Cells["E1"].Value = "馈电下行链路空间衰减（dB）";
                    worksheet.Cells["F1"].Value = "用户上行链路空间衰减(dB)";
                    worksheet.Cells["G1"].Value = "终端输出EIRP(dBw)";
                    worksheet.Cells["H1"].Value = "载荷相控阵接收端单波束PFD(dBw/m2)";
                    worksheet.Cells["I1"].Value = "载荷QV天线发射EIRP值(dBw)";
                    worksheet.Cells["J1"].Value = "载荷相控阵接收端信噪比（dB）";
                    worksheet.Cells["K1"].Value = "信关站接收中频功率（dBm)";
                    worksheet.Cells["L1"].Value = "信关站下行接收信噪比(dB)";
                    worksheet.Cells["M1"].Value = "反向链路C/(N+I)信干噪比值（dB)";
                    worksheet.Cells["N1"].Value = "反向多普勒频偏";
                    worksheet.Cells["O1"].Value = "时间";
                    worksheet = workbook.Worksheets.Add("随路QV");
                    worksheet.Cells["A1"].Value = "馈电下行链路空间衰减(dB)";
                    worksheet.Cells["B1"].Value = "信关站接收中频功率值(dBm)";
                    worksheet.Cells["C1"].Value = "时间";

                }

                // 获取工作表
                ExcelWorksheet existingWorksheet = package.Workbook.Worksheets["Sheet1"];

                // 获取下一个空行的行号
                int nextRow = existingWorksheet.Dimension.Rows + 1;

                for (int i = start; i < end; i++)
                {
                    existingWorksheet.Cells[nextRow, 1].Value = numlist[i].x;
                    existingWorksheet.Cells[nextRow, 2].Value = numlist[i].y;
                    existingWorksheet.Cells[nextRow, 3].Value = numlist[i].z;
                    existingWorksheet.Cells[nextRow, 4].Value = numlist[i].vx;
                    existingWorksheet.Cells[nextRow, 5].Value = numlist[i].vy;
                    existingWorksheet.Cells[nextRow, 6].Value = numlist[i].vz;
                    existingWorksheet.Cells[nextRow, 7].Value = numlist[i].ElevationDown;
                    existingWorksheet.Cells[nextRow, 8].Value = numlist[i].ElevationUp;
                    existingWorksheet.Cells[nextRow, 9].Value = numlist[i].Azimuth;
                    existingWorksheet.Cells[nextRow, 10].Value = numlist[i].lu_distance;
                    existingWorksheet.Cells[nextRow, 11].Value = numlist[i].ku_up;
                    existingWorksheet.Cells[nextRow, 12].Value = numlist[i].ku_down;
                    existingWorksheet.Cells[nextRow, 13].Value = numlist[i].lf_distance;
                    existingWorksheet.Cells[nextRow, 14].Value = numlist[i].qv_up;
                    existingWorksheet.Cells[nextRow, 15].Value = numlist[i].qv_down;
                    existingWorksheet.Cells[nextRow, 16].Value = numlist[i].qv_sway;
                    existingWorksheet.Cells[nextRow, 17].Value = numlist[i].Time.ToString();
                    nextRow++;
                }

                existingWorksheet = package.Workbook.Worksheets["前向"];

                // 获取下一个空行的行号
                nextRow = existingWorksheet.Dimension.Rows + 1;

                for (int i = start; i < end; i++)
                {
                    existingWorksheet.Cells[nextRow, 1].Value = downLinkDatas[i].ElevationDown;
                    existingWorksheet.Cells[nextRow, 2].Value = downLinkDatas[i].lf_distance;
                    existingWorksheet.Cells[nextRow, 3].Value = downLinkDatas[i].ElevationDown;
                    existingWorksheet.Cells[nextRow, 4].Value = downLinkDatas[i].lu_distance;
                    existingWorksheet.Cells[nextRow, 5].Value = downLinkDatas[i].QVUp;
                    existingWorksheet.Cells[nextRow, 6].Value = downLinkDatas[i].kuDown;
                    existingWorksheet.Cells[nextRow, 7].Value = downLinkDatas[i].InPower;
                    existingWorksheet.Cells[nextRow, 8].Value = downLinkDatas[i].EIRP;
                    existingWorksheet.Cells[nextRow, 9].Value = downLinkDatas[i].SNR;
                    existingWorksheet.Cells[nextRow, 10].Value = downLinkDatas[i].UPEIRP;
                    existingWorksheet.Cells[nextRow, 11].Value = downLinkDatas[i].receiveGain;
                    existingWorksheet.Cells[nextRow, 12].Value = downLinkDatas[i].IFpower;
                    existingWorksheet.Cells[nextRow, 13].Value = downLinkDatas[i].downSNR;
                    existingWorksheet.Cells[nextRow, 14].Value = downLinkDatas[i].dryingRatio;
                    existingWorksheet.Cells[nextRow, 15].Value = downtDs[i].number;
                    existingWorksheet.Cells[nextRow, 16].Value = numlist[i].Time.ToString();
                    nextRow++;
                }

                existingWorksheet = package.Workbook.Worksheets["反向"];

                // 获取下一个空行的行号
                nextRow = existingWorksheet.Dimension.Rows + 1;

                for (int i = start; i < end; i++)
                {
                    existingWorksheet.Cells[nextRow, 1].Value = upLinkDatas[i].ElevationDown;
                    existingWorksheet.Cells[nextRow, 2].Value = upLinkDatas[i].lf_distance;
                    existingWorksheet.Cells[nextRow, 3].Value = upLinkDatas[i].ElevationDown;
                    existingWorksheet.Cells[nextRow, 4].Value = upLinkDatas[i].lu_distance;
                    existingWorksheet.Cells[nextRow, 5].Value = upLinkDatas[i].QVDown;
                    existingWorksheet.Cells[nextRow, 6].Value = upLinkDatas[i].KuUp;
                    existingWorksheet.Cells[nextRow, 7].Value = upLinkDatas[i].EIRP;
                    existingWorksheet.Cells[nextRow, 8].Value = upLinkDatas[i].FPD;
                    existingWorksheet.Cells[nextRow, 9].Value = upLinkDatas[i].QVEIRP;
                    existingWorksheet.Cells[nextRow, 10].Value = upLinkDatas[i].receiveSNR;
                    existingWorksheet.Cells[nextRow, 11].Value = upLinkDatas[i].IFpower;
                    existingWorksheet.Cells[nextRow, 12].Value = upLinkDatas[i].downSNR;
                    existingWorksheet.Cells[nextRow, 13].Value = upLinkDatas[i].dryRatio;
                    existingWorksheet.Cells[nextRow, 14].Value = uptDs[i].number;
                    existingWorksheet.Cells[nextRow, 15].Value = numlist[i].Time.ToString();
                    nextRow++;
                }
                existingWorksheet = package.Workbook.Worksheets["随路QV"];

                // 获取下一个空行的行号
                nextRow = existingWorksheet.Dimension.Rows + 1;

                for (int i = start; i < end; i++)
                {
                    existingWorksheet.Cells[nextRow, 1].Value = swayDatas[i].spaceAttenuation;
                    existingWorksheet.Cells[nextRow, 2].Value = swayDatas[i].IFpower;
                    existingWorksheet.Cells[nextRow, 3].Value = numlist[i].Time.ToString();
                    nextRow++;
                }

                // 保存Excel文件
                package.Save();
            }
        }

        public void insertExcel(string folderPath,string FILENAME, List<TD> tDs)
        {
            string directiory = folderPath;

            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            // 创建或打开Excel文件
            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                // 如果文件不存在，则创建新的工作簿和工作表
                if (!fileExists)
                {
                    ExcelWorkbook workbook = package.Workbook;
                    ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");

                    //// 写入标题行
                    //worksheet.Cells["A1"].Value = "A";
                    //worksheet.Cells["B1"].Value = "TDu";
                    //worksheet.Cells["C1"].Value = "Time";

                }

                // 获取工作表
                ExcelWorksheet existingWorksheet = package.Workbook.Worksheets["Sheet1"];

                // 获取下一个空行的行号
                int nextRow = existingWorksheet.Dimension.Rows + 1;
                int num = 0;
                foreach (var value in tDs)
                {
                    existingWorksheet.Cells[nextRow, 1].Value = "A = " + num.ToString();
                    existingWorksheet.Cells[nextRow, 2].Value = value.number;
                    existingWorksheet.Cells[nextRow, 3].Value = value.Time.ToString();
                    num++;
                    nextRow++;
                }

                // 保存Excel文件
                package.Save();
            }
        }

        public void insertExcel(string folderPath, string FILENAME, List<TD> tDs,int startId, int lastId)
        {
            string directiory = folderPath;

            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            // 创建或打开Excel文件
            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                // 如果文件不存在，则创建新的工作簿和工作表
                if (!fileExists)
                {
                    ExcelWorkbook workbook = package.Workbook;
                    ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");

                    //// 写入标题行
                    //worksheet.Cells["A1"].Value = "A";
                    //worksheet.Cells["B1"].Value = "TDf";
                    //worksheet.Cells["C1"].Value = "Time";

                }

                // 获取工作表
                ExcelWorksheet existingWorksheet = package.Workbook.Worksheets["Sheet1"];

                // 获取下一个空行的行号
                int nextRow = existingWorksheet.Dimension.Rows + 1;
                int num = 0;
                for (int i = startId; i <= lastId; i ++)
                {
                    existingWorksheet.Cells[nextRow, 1].Value = "A = " + num.ToString();
                    existingWorksheet.Cells[nextRow, 2].Value = tDs[i].number;
                    existingWorksheet.Cells[nextRow, 3].Value = tDs[i].Time.ToString();
                    num++;
                    nextRow++;
                }

                // 保存Excel文件
                package.Save();
            }
        }

        public void insertExcel(string folderPath, string FILENAME, List<TD> tDs, List<TD> deltas, int startId, int lastId)
        {
            string directiory = folderPath;

            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            // 创建或打开Excel文件
            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                // 如果文件不存在，则创建新的工作簿和工作表
                if (!fileExists)
                {
                    ExcelWorkbook workbook = package.Workbook;
                    ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");

                    //// 写入标题行
                    //worksheet.Cells["A1"].Value = "A";
                    //worksheet.Cells["B1"].Value = "TDf";
                    //worksheet.Cells["C1"].Value = "Time";

                }

                // 获取工作表
                ExcelWorksheet existingWorksheet = package.Workbook.Worksheets["Sheet1"];

                // 获取下一个空行的行号
                int nextRow = existingWorksheet.Dimension.Rows + 1;
                int num = 0;
                for (int i = startId; i <= lastId; i++)
                {
                    existingWorksheet.Cells[nextRow, 1].Value = "A = " + num.ToString() + "," + tDs[i].number.ToString("0.000000000") + "," + deltas[i].number.ToString("0.000");
                    existingWorksheet.Cells[nextRow, 2].Value = tDs[i].Time.ToString();
                    num++;
                    nextRow++;
                }

                // 保存Excel文件
                package.Save();
            }
        }


        public void insertExcel(string folderPath, string FILENAME, double deltafu, double deltaff, double TDu, double TDf)
        {
            string directiory = folderPath;

            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            // 创建或打开Excel文件
            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                // 如果文件不存在，则创建新的工作簿和工作表
                if (!fileExists)
                {
                    ExcelWorkbook workbook = package.Workbook;
                    ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");

                    // 写入标题行
                    worksheet.Cells["A1"].Value = "deltafu";
                    worksheet.Cells["B1"].Value = "deltaff";
                    worksheet.Cells["C1"].Value = "count";
                    worksheet.Cells["D1"].Value = "TDu";
                    worksheet.Cells["E1"].Value = "TDf";
                    worksheet.Cells["F1"].Value = "count";

                }

                // 获取工作表
                ExcelWorksheet existingWorksheet = package.Workbook.Worksheets["Sheet1"];

                // 获取下一个空行的行号
                int nextRow = existingWorksheet.Dimension.Rows + 1;

                //foreach (numTest value in numlist)
                //{
                existingWorksheet.Cells[nextRow, 1].Value = deltafu;
                existingWorksheet.Cells[nextRow, 2].Value = deltaff;
                existingWorksheet.Cells[nextRow, 3].Value = deltafu + deltaff;
                existingWorksheet.Cells[nextRow, 4].Value = TDu;
                existingWorksheet.Cells[nextRow, 5].Value = TDf;
                existingWorksheet.Cells[nextRow, 6].Value = TDu + TDf;

                nextRow++;
                //}

                // 保存Excel文件
                package.Save();
            }
        }

        public void insertExcel(string folderPath, string FILENAME, List<TD> tds, List<TD> deltas)
        {
            string directiory = folderPath;

            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                // 如果文件不存在，则创建新的工作簿和工作表
                if (!fileExists)
                {
                    ExcelWorkbook workbook = package.Workbook;
                    ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");

                    //// 写入标题行
                    //worksheet.Cells["A1"].Value = "A";
                    //worksheet.Cells["B1"].Value = "TDu";
                    //worksheet.Cells["C1"].Value = "Time";

                }

                // 获取工作表
                ExcelWorksheet existingWorksheet = package.Workbook.Worksheets["Sheet1"];

                // 获取下一个空行的行号
                int nextRow = existingWorksheet.Dimension.Rows + 1;
                int num = 0;
                for (int i = 0; i < tds.Count; i++)
                {
                    existingWorksheet.Cells[nextRow, 1].Value = "A = " + num.ToString();
                    existingWorksheet.Cells[nextRow, 2].Value = tds[i].number.ToString("0.000000000");
                    existingWorksheet.Cells[nextRow, 3].Value = deltas[i].number.ToString("0.000");
                    existingWorksheet.Cells[nextRow, 4].Value = tds[i].Time.ToString();
                    num++;
                    nextRow++;
                }

                // 保存Excel文件
                package.Save();
            }

        }

        public void insertExcel( string FILENAME, List<BsSatData> bsSatDatas)
        {
            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            // 创建或打开Excel文件
            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                // 如果文件不存在，则创建新的工作簿和工作表
                if (!fileExists)
                {
                    ExcelWorkbook workbook = package.Workbook;
                    ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");

                    // 写入标题行
                    worksheet.Cells["A1"].Value = "连接卫星SatID";
                    worksheet.Cells["B1"].Value = "连接卫星天线编号";
                    worksheet.Cells["C1"].Value = "连接天伺馈编号";
                    worksheet.Cells["D1"].Value = "开始UTC时刻";
                    worksheet.Cells["E1"].Value = "持续时长";

                }

                // 获取工作表
                ExcelWorksheet existingWorksheet = package.Workbook.Worksheets["Sheet1"];

                // 获取下一个空行的行号
                int nextRow = existingWorksheet.Dimension.Rows + 1;
                int num = 1;
                foreach (var value in bsSatDatas)
                {
                    existingWorksheet.Cells[nextRow, 1].Value = value.SatId;
                    existingWorksheet.Cells[nextRow, 2].Value = value.SatelliteAntennal;
                    existingWorksheet.Cells[nextRow, 3].Value = value.BaseStationFeeder;
                    existingWorksheet.Cells[nextRow, 4].Value = value.FeederEpochUTC;
                    existingWorksheet.Cells[nextRow, 5].Value = value.FeederDuration;

                    nextRow++;
                }

                // 保存Excel文件
                package.Save();
            }
        }

        public void insertExcel( string FILENAME, List<BsTxData> bsTxDatas)
        {

            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            // 创建或打开Excel文件
            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                // 如果文件不存在，则创建新的工作簿和工作表
                if (!fileExists)
                {
                    ExcelWorkbook workbook = package.Workbook;
                    ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");

                    //// 写入标题行
                    worksheet.Cells["A1"].Value = "连接格网编号";
                    worksheet.Cells["B1"].Value = "服务开始UTC时刻";
                    worksheet.Cells["C1"].Value = "服务持续时长";
                    worksheet.Cells["D1"].Value = "该通道对应的频点信息";
                    worksheet.Cells["E1"].Value = "发射功率";
                    worksheet.Cells["F1"].Value = "Grid1卫星切换开始UTC时刻";
                    worksheet.Cells["G1"].Value = "发生切换后连接基站编号";
                    worksheet.Cells["H1"].Value = "发生切换后连接的卫星SatID";

                }

                // 获取工作表
                ExcelWorksheet existingWorksheet = package.Workbook.Worksheets["Sheet1"];

                // 获取下一个空行的行号
                int nextRow = existingWorksheet.Dimension.Rows + 1;
                foreach (var value in bsTxDatas)
                {
                    existingWorksheet.Cells[nextRow, 1].Value =value.Grid;
                    existingWorksheet.Cells[nextRow, 2].Value = value.ServiceEpochUTC;
                    existingWorksheet.Cells[nextRow, 3].Value = value.ServiceDuration;
                    existingWorksheet.Cells[nextRow, 4].Value = value.Freq;
                    existingWorksheet.Cells[nextRow, 5].Value = value.Power;
                    existingWorksheet.Cells[nextRow, 6].Value = value.SatelliteSwitchUTC;
                    existingWorksheet.Cells[nextRow, 7].Value = value.RelaygNodeB;
                    existingWorksheet.Cells[nextRow, 8].Value = value.RelaySatId;
                    nextRow++;
                }

                // 保存Excel文件
                package.Save();
            }
        }

        public void insertExcel( string FILENAME, Dictionary<string, List<GridBsData>> GBsdictonarys)
        {
            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            // 创建或打开Excel文件
            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                foreach (var item in GBsdictonarys)
                {
                    // 如果文件不存在，则创建新的工作簿和工作表
                    if (!fileExists)
                    {
                        ExcelWorkbook workbook = package.Workbook;
                        ExcelWorksheet worksheet = workbook.Worksheets.Add(item.Key);

                        //// 写入标题行
                        worksheet.Cells["A1"].Value = "开始UTC时刻";
                        worksheet.Cells["B1"].Value = "持续时长";
                        worksheet.Cells["C1"].Value = "对应的小区编号";
                        worksheet.Cells["D1"].Value = "该小区对应的TAC";                       
                    }

                    // 获取工作表
                    ExcelWorksheet existingWorksheet = package.Workbook.Worksheets[item.Key];

                    // 获取下一个空行的行号
                    int nextRow = existingWorksheet.Dimension.Rows + 1;
                    foreach (var value in item.Value)
                    {
                        existingWorksheet.Cells[nextRow, 1].Value = value.ServiceEpochUTC;
                        existingWorksheet.Cells[nextRow, 2].Value = value.ServiceDuration;
                        existingWorksheet.Cells[nextRow, 3].Value = value.Cell;
                        existingWorksheet.Cells[nextRow, 4].Value = value.Cell1TAC;                       
                        nextRow++;
                    }

                }
                // 保存Excel文件
                package.Save();
            }
        }

        public List<numTest> readfromExcel(string filePath)
        { 
            List<numTest> numTests = new List<numTest>();
            using (ExcelPackage package = new ExcelPackage(new FileInfo(filePath)))
            {
                ExcelWorkbook workbook = package.Workbook;
                if (workbook != null)
                {
                    ExcelWorksheet worksheet = workbook.Worksheets["Sheet1"];
                    int rowCount = worksheet.Dimension.Rows;
                    for (int i = 2; i < rowCount; i++)
                    { 
                        numTest numTest = new numTest();
                        numTest.x = double.Parse(worksheet.Cells[i, 5].Value.ToString());
                        numTest.y = double.Parse(worksheet.Cells[i, 6].Value.ToString());
                        numTest.z = double.Parse(worksheet.Cells[i, 7].Value.ToString());
                        numTest.vx = double.Parse(worksheet.Cells[i, 8].Value.ToString());
                        numTest.vy = double.Parse(worksheet.Cells[i, 9].Value.ToString());
                        numTest.vz = double.Parse(worksheet.Cells[i, 10].Value.ToString());
                        numTest.can_sec = double.Parse(worksheet.Cells[i, 3].Value.ToString());
                        numTests.Add(numTest);
                    }
                }
            }
            return numTests;
        }

        public void insertExcel(string folderPath, string FILENAME, List<TD> tdu, List<TD> tdf, List<numTest> numlist, location location)
        {
            string directiory = folderPath;

            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                // 如果文件不存在，则创建新的工作簿和工作表
                if (!fileExists)
                {

                    ExcelWorkbook workbook = package.Workbook;
                    ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");

                    // 写入标题行
                    worksheet.Cells["A1"].Value = "can_sec";
                    worksheet.Cells["B1"].Value = "X";
                    worksheet.Cells["C1"].Value = "Y";
                    worksheet.Cells["D1"].Value = "Z";
                    worksheet.Cells["E1"].Value = "VX";
                    worksheet.Cells["F1"].Value = "VY";
                    worksheet.Cells["G1"].Value = "VZ";
                    worksheet.Cells["H1"].Value = "tdu";
                    worksheet.Cells["I1"].Value = "tdf";
                    worksheet.Cells["J1"].Value = "TD";
                    worksheet.Cells["K1"].Value = "x_st";
                    worksheet.Cells["L1"].Value = "y_st";
                    worksheet.Cells["M1"].Value = "z_st";
                    worksheet.Cells["N1"].Value = "x_bs";
                    worksheet.Cells["O1"].Value = "y_st";
                    worksheet.Cells["P1"].Value = "z_st";
                    worksheet.Cells["Q1"].Value = "TIME";
                }

                // 获取工作表
                ExcelWorksheet existingWorksheet = package.Workbook.Worksheets["Sheet1"];

                // 获取下一个空行的行号
                int nextRow = existingWorksheet.Dimension.Rows + 1;
                int num = 1;
                for (int i = 0; i < tdu.Count; i++)
                {
                    existingWorksheet.Cells[nextRow, 1].Value = numlist[i].can_sec;
                    existingWorksheet.Cells[nextRow, 2].Value = numlist[i].x;
                    existingWorksheet.Cells[nextRow, 3].Value = numlist[i].y;
                    existingWorksheet.Cells[nextRow, 4].Value = numlist[i].z;
                    existingWorksheet.Cells[nextRow, 5].Value = numlist[i].vx;
                    existingWorksheet.Cells[nextRow, 6].Value = numlist[i].vy;
                    existingWorksheet.Cells[nextRow, 7].Value = numlist[i].vz;
                    existingWorksheet.Cells[nextRow, 8].Value = tdu[i].number;
                    existingWorksheet.Cells[nextRow, 9].Value = tdf[i].number;
                    existingWorksheet.Cells[nextRow, 10].Value = tdu[i].number + tdf[i].number;
                    existingWorksheet.Cells[nextRow, 11].Value = location.x_st;
                    existingWorksheet.Cells[nextRow, 12].Value = location.y_st;
                    existingWorksheet.Cells[nextRow, 13].Value = location.z_st;
                    existingWorksheet.Cells[nextRow, 14].Value = location.x_bs;
                    existingWorksheet.Cells[nextRow, 15].Value = location.y_bs;
                    existingWorksheet.Cells[nextRow, 16].Value = location.z_bs;
                    existingWorksheet.Cells[nextRow, 17].Value = tdu[i].Time.ToString();

                    num++;
                    nextRow++;
                }

                // 保存Excel文件
                package.Save();
            }

        }

        public void insertExcel(string folderPath, string FILENAME, List<TD> tdu, List<TD> tdf, List<location> locations)
        {
            string directiory = folderPath;

            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                // 如果文件不存在，则创建新的工作簿和工作表
                if (!fileExists)
                {

                    ExcelWorkbook workbook = package.Workbook;
                    ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");

                    // 写入标题行
                    worksheet.Cells["A1"].Value = "tdu";
                    worksheet.Cells["B1"].Value = "tdf";
                    worksheet.Cells["C1"].Value = "TD";
                    worksheet.Cells["D1"].Value = "x_st";
                    worksheet.Cells["E1"].Value = "y_st";
                    worksheet.Cells["F1"].Value = "z_st";
                    worksheet.Cells["G1"].Value = "x_bs";
                    worksheet.Cells["H1"].Value = "y_st";
                    worksheet.Cells["I1"].Value = "z_st";
                    worksheet.Cells["J1"].Value = "TIME";                   
                }

                // 获取工作表
                ExcelWorksheet existingWorksheet = package.Workbook.Worksheets["Sheet1"];

                // 获取下一个空行的行号
                int nextRow = existingWorksheet.Dimension.Rows + 1;
                int num = 1;
                for (int i = 0; i < tdu.Count; i++)
                {
                    existingWorksheet.Cells[nextRow, 1].Value = tdu[i].number;
                    existingWorksheet.Cells[nextRow, 2].Value = tdf[i].number;
                    existingWorksheet.Cells[nextRow, 3].Value = tdu[i].number + tdf[i].number;
                    existingWorksheet.Cells[nextRow, 4].Value = locations[i].x_st;
                    existingWorksheet.Cells[nextRow, 5].Value = locations[i].y_st;
                    existingWorksheet.Cells[nextRow, 6].Value = locations[i].z_st;
                    existingWorksheet.Cells[nextRow, 7].Value = locations[i].x_bs;
                    existingWorksheet.Cells[nextRow, 8].Value = locations[i].y_bs;
                    existingWorksheet.Cells[nextRow, 9].Value = locations[i].z_bs;
                    existingWorksheet.Cells[nextRow, 10].Value = tdu[i].Time.ToString();

                    num++;
                    nextRow++;
                }

                // 保存Excel文件
                package.Save();
            }

        }

        public void insertExcel(string FILENAME, List<PassInfo> datas)
        {
            string fileName = FILENAME + ".xlsx";
            string currentfilePath = Path.Combine(directiory, fileName);

            bool fileExists = File.Exists(currentfilePath);

            // 创建或打开Excel文件
            using (var package = new ExcelPackage(new FileInfo(currentfilePath)))
            {
                // 如果文件不存在，则创建新的工作簿和工作表
                if (!fileExists)
                {
                    ExcelWorkbook workbook = package.Workbook;
                    ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");

                    // 写入标题行
                    worksheet.Cells["A1"].Value = "卫星Id";
                    worksheet.Cells["B1"].Value = "卫星名称";
                    worksheet.Cells["C1"].Value = "测控站id";
                    worksheet.Cells["D1"].Value = "测控站名称";
                    worksheet.Cells["E1"].Value = "开始时间，utc时间";
                    worksheet.Cells["F1"].Value = "结束时间，utc时间";
                    worksheet.Cells["G1"].Value = "持续时长，单位秒";
                    worksheet.Cells["H1"].Value = "干扰隔离开始时间，utc时间";
                    worksheet.Cells["I1"].Value = "干扰隔离结束时间，utc时间";
                    worksheet.Cells["J1"].Value = "开始xyz坐标，double数组";
                    worksheet.Cells["K1"].Value = "结束xyz坐标，double数组";

                }

                // 获取工作表
                ExcelWorksheet existingWorksheet = package.Workbook.Worksheets["Sheet1"];

                // 获取下一个空行的行号
                int nextRow = existingWorksheet.Dimension.Rows + 1;
                int num = 1;
                foreach (var value in datas)
                {
                    existingWorksheet.Cells[nextRow, 1].Value = value.scId;
                    existingWorksheet.Cells[nextRow, 2].Value = value.satName;
                    existingWorksheet.Cells[nextRow, 3].Value = value.gateWayId;
                    existingWorksheet.Cells[nextRow, 4].Value = value.gateWayName;
                    existingWorksheet.Cells[nextRow, 5].Value = value.start.Replace("T", " ");
                    existingWorksheet.Cells[nextRow, 6].Value = value.end.Replace("T", " ");
                    existingWorksheet.Cells[nextRow, 7].Value = value.duration;
                    existingWorksheet.Cells[nextRow, 8].Value = value.gsoStart.Replace("T", " ");
                    existingWorksheet.Cells[nextRow, 9].Value = value.gsoEnd.Replace("T", " ");
                    existingWorksheet.Cells[nextRow, 10].Value = string.Join(",", value.startXYZ);
                    existingWorksheet.Cells[nextRow, 11].Value = string.Join(",", value.endXYZ);

                    nextRow++;
                }

                // 保存Excel文件
                package.Save();

                MessageBox.Show(directiory);
            }
        }

    }
}
